﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Mimoza.RdpPlugin
{
    class ExecutePlugin : Mimoza.PluginLayer.IExecutePlugin
    {
        const string c_pluginFileExt = "rdp";

        public ExecutePlugin()
        {
            m_pluginFileSync = new PluginFileSync(c_pluginFileExt);

            PluginFileSync.Do(RdpFilesFolder);

            SyncStartupFiles();
        }

        public PluginFileSync PluginFileSync
        {
            get { return m_pluginFileSync; }
        }

        #region IExecutePlugin Members

        public string PluginId
        {
            get { return this.ToString(); }
        }

        public void Add(Mimoza.ObjectLayer.Policy policy)
        {
            Add(policy, GetData(policy.Data));
        }

        public void Remove(Mimoza.ObjectLayer.Policy policy)
        {
            Remove(policy, GetData(policy.Data));
        }

        public void Update(Mimoza.ObjectLayer.Policy policy)
        {
            Update(policy, GetData(policy.Data));
        }

        #endregion

        void Add(Mimoza.ObjectLayer.Policy policy, RdpData data)
        {
            Trace("Mimoza.RdpPlugin.ExecutePlugin.Add Name - '" + policy.Name + 
                            "' Computer - '" + data.Computer +
                            "' User - '" + data.UserName + "'.");

            string rdpFileName = BuildRdpFileName(policy, data);

            RdpFile rdpFile = new RdpFile();
            rdpFile.CreateFile(rdpFileName, data, policy.ID);

            PluginFileSync.Replace(rdpFileName);

            if (!data.IsPersistentShortcut) File.Delete(rdpFileName);

            if (data.IsStartOnStartup) SyncStartupFiles();
        }

        void Remove(Mimoza.ObjectLayer.Policy policy, RdpData data)
        {
            Trace("Mimoza.RdpPlugin.ExecutePlugin.Remove Name - '" + policy.Name +
                            "' Computer - '" + data.Computer +
                            "' User - '" + data.UserName + "'.");

            RemoveRdpFileByName(policy, data, true);

            if (data.IsStartOnStartup) SyncStartupFiles();
        }

        void Update(Mimoza.ObjectLayer.Policy policy, RdpData data)
        {
            Trace("Mimoza.RdpPlugin.ExecutePlugin.Update Name - '" + policy.Name +
                            "' Computer - '" + data.Computer +
                            "' User - '" + data.UserName + "'.");

            RemoveRdpFileByName(policy, data, false);
            Add(policy, data);
        }

        private static void SyncStartupFiles()
        {
            StartupFileSync startupFileSync = new StartupFileSync(Common.GlobalSettings.StartupPath, c_pluginFileExt);
            startupFileSync.Do(RdpFilesFolder);
        }

        private static string BuildRdpFileName(Mimoza.ObjectLayer.Policy policy, RdpData data)
        {
            string filePath = RdpFilesFolder;
            string fileName = Path.Combine(filePath, data.SessionName + "." + policy.ID.ToString());

            return fileName;
        }

        private static string RdpFilesFolder
        {
            get
            {
                return Path.Combine(Common.GlobalSettings.DataPath, "rdp");
            }
        }

        private void RemoveRdpFileByName(Mimoza.ObjectLayer.Policy policy, RdpData data,
            bool syncDesktopFlag)
        {
            string[] rdpFiles = Directory.GetFiles(RdpFilesFolder, "*." + policy.ID.ToString());
            string rdpFileName = (rdpFiles != null && rdpFiles.Length > 0) ? rdpFiles[0] : "";

            if (!string.IsNullOrEmpty(rdpFileName) && System.IO.File.Exists(rdpFileName))
            {
                Common.Logger.Log.Info("Mimoza.RdpPlugin.ExecutePlugin: delete rdp file name - '" + 
                    rdpFileName + "'.");

                System.IO.File.Delete(rdpFileName);

                if (syncDesktopFlag) PluginFileSync.Remove(rdpFileName);
            }
        }

        void Trace(string outStr)
        {
            Common.Logger.Log.Trace(outStr);
            //System.Windows.Forms.MessageBox.Show(outStr);
        }

        RdpData GetData(string data)
        {
            return RdpData.Deserialize(data);
        }

        PluginFileSync m_pluginFileSync;
    }
}
